package myJs.detailInfo

import japgolly.scalajs.react.ScalaFnComponent
import japgolly.scalajs.react.vdom.all._
import myJs.Implicits._
import myJs.api.Api
import myJs.myPkg.Hooks._
import myJs.tool.Tool
import shared.pojo.Browse._

/** Created by yz on 28/11/2023
  */
object AssemblySummaryCp {

  case class Props(basicData: AssemblyInfo)

  val Component = ScalaFnComponent[Props] { props =>

    val basicData = props.basicData

    object FTool {}

    div(
      h4("Assembly Summary"),
      h5(
        fontWeight.bold,
        marginTop := 10.px,
        basicData.name
      ),
      div(
        className := "table-responsive",
        marginTop := 10.px,
        table(
          id := "table",
          className := "table table-striped table-hover table-bordered",
          tableLayout.fixed,
          wordWrap.breakWord,
          tbody(
            basicData.summary.obj.value.toList.toVdomArray { case (header, uV) =>
              val v = uV.str
              val trueV = if (v.isDouble) {
                if (v.isInt) {
                  v
                } else {
                  v.toDouble.toFixed(Tool.decDigits)
                }
              } else v
              val inValidNames = List(
                "Not collected",
                "Not applicable",
                "NA",
                "missing"
              )
              val finalV = header match {
                case "BioSample" =>
                  if (inValidNames.containsIgnoreCase(trueV)) {
                    span(trueV)
                  } else {
                    a(
                      target.blank,
                      href := s"https://www.ncbi.nlm.nih.gov/biosample/${trueV}/",
                      trueV
                    )
                  }
                case "BioProject" =>
                  if (inValidNames.containsIgnoreCase(trueV)) {
                    span(trueV)
                  } else {
                    a(
                      target.blank,
                      href := s"https://www.ncbi.nlm.nih.gov/bioproject/${trueV}/",
                      trueV
                    )
                  }
                case "WGS Project" =>
                  if (inValidNames.containsIgnoreCase(trueV)) {
                    span(trueV)
                  } else {
                    val r = "^([^\\(\\)]+)\\(([^\\(\\)]+)\\)$".r
                    val matcher = r.findAllIn(trueV)
                    val (v, id) = (matcher.group(1), matcher.group(2))
                    a(
                      target.blank,
                      href := s"https://www.ncbi.nlm.nih.gov/nuccore/${id}",
                      v
                    )
                  }
                case _ => span(trueV)
              }
              tr(
                key := s"${header}",
                th(header),
                td(finalV)
              )
            }
          )
        )
      )
    )

  }

  def apply(basicData: AssemblyInfo) = {
    Component(Props(basicData))
  }

}
